﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        Catelist.aspx.cs           
/// Author :         Wangbc
/// CDT:             2013-02-01           
/// Version:         1.0.0.0                
/// Depiction:       课件分类查询   
/// </summary>
public partial class main_admin_CateList : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.strHc = "0";
        //设置母版页控件显示
        ((Literal)Master.FindControl("lblpath")).Text = MenuPath;

        EM = Request.QueryString[WEBConstants.EM_QUERY_STRING];

        //初始化EM
        if (!Cit.CDE.Net.Module.Commons.CDEConstants.IsEmpty(EM))
        {
            em = cde.cache.CDEFactory.getEMByFullName(EM);
        }
        else
        {
            em = null;
        }

        Master.sc = this.CreatSc(em);

        //设置母版页控件事件
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ItemDataBound += new DataGridItemEventHandler(this.itemDataBound);


    }
    /// <summary>
    /// 操作事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void itemDataBound(object sender, DataGridItemEventArgs e)
    {
        //控制母版页控件显示内容
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns[((SimpleResultDataGrid)Master.FindControl("dgmeisai")).Columns.Count - 1].Visible = true;
    }

    /// <summary>
    /// 删除操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdDel_Click(object sender, EventArgs e)
    {

        string strErr = string.Empty;
        string strMsg = string.Empty;
        //获得当前显示列表
        List<CDEEntity> S_listEntity = (List<CDEEntity>)Session[WEBConstants.LIST_SESSION_ID + EM];
        List<CDEEntity> listEntity = new List<CDEEntity>();
        foreach (int index in Master.listIndex)
        {
            CDEEntity entity = S_listEntity[index];
            string cate_Id = Util.objectToString(entity.get("CATE_ID"));


           //查询是否还有课件正在使用这个类型
            CDEEntityManager userEm = CDEEMFactory.getEMByFullName("WEB.CSW_MANAGE");
            CDESearchCriteria userSc = new CDESearchCriteria(userEm);
            
            //查询条件OR　设置
            List<CDESearchCriteria> CDESearchCriteria = new List<CDESearchCriteria>();
            CDESearchCriteria sc1 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc1.addCondition("CONTENT_TYPE", cate_Id);
            CDESearchCriteria.Add(sc1);

            CDESearchCriteria sc2 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc2.addCondition("SUBJECT_TYPE", cate_Id);
            CDESearchCriteria.Add(sc2);

            CDESearchCriteria sc3 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc3.addCondition("CLASS_TYPE", cate_Id);
            CDESearchCriteria.Add(sc3);

            CDESearchCriteria sc4 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc4.addCondition("ELECTIVE_TYPE", cate_Id);
            CDESearchCriteria.Add(sc4);

            CDESearchCriteria sc5 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc5.addCondition("BOOKS_TYPE", cate_Id);
            CDESearchCriteria.Add(sc5);

            CDESearchCriteria sc6 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc6.addCondition("VERSION_TYPE", cate_Id);
            CDESearchCriteria.Add(sc6);

            CDESearchCriteria sc7 = CDEEMServiceFactory.getEMService(em).CreateSearchCriteria();
            sc7.addCondition("OTHER_TYPE", cate_Id);
            CDESearchCriteria.Add(sc7);

           userSc.addORCondition(CDESearchCriteria.ToArray());

            List<CDEEntity> userList = CDEService.LoadEntities(userEm, userSc);

            if (userList.Count > 0)
            {
                showMessageErrAndOk("课件分类正在使用！");

                Master.dispSortImg();

                return;
            }

            //删除
            S_listEntity[index].Operation = CDEDataOperation.UPDATE;
            S_listEntity[index].set("DEL_FLG", "1");

            listEntity.Add(S_listEntity[index]);
        }
        //更新课件分类信息
        CDEService.UpdateEntities(em, listEntity);

        foreach (int index in Master.listIndex)
        {
            S_listEntity.Remove(S_listEntity[index]);
        }
        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).ListEntity = S_listEntity;
        //更新当前显示列表
        Session[WEBConstants.LIST_SESSION_ID + EM] = S_listEntity;
        //设置序号
        Master.GetFirstNum();

        ((SimpleResultDataGrid)Master.FindControl("dgmeisai")).DataBind();
        //判断更新课件分类信息结果
        for (int index = 0; index < listEntity.Count; index++)
        {
            if (listEntity[index].Result == CDEDataOperationResult.ERROR)
            {
                strErr = strErr + (((main_main_MasterPage)this.Master).listIndex[index] + 1).ToString() + ",";
            }
        }
        //根据错误信息返回结果
        if (strErr != string.Empty)
        {
            strErr = strErr.Substring(0, strErr.Length - 1);
            strMsg = Resources.ResourceErrMsg.DELETE_ERR;
            strMsg = strMsg.Replace("{0}", strErr);
            showMessageErrAndOk(strMsg);
        }
        else
        {
            //系统日志
            // SysLog.AddSysLog(listEntity, this.GetLoginOprator());

            showMessageErrAndOk(Resources.ResourceAdmin.MSG_ROLE_DEL_RESULT_OK);
        }
        Master.refreshPage();
        Master.refreshResult();
        
    }
}
